//
//  SpecialTableViewController.m
//  老司机发车了
//
//  Created by 程成 on 16/8/8.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "SpecialTableViewController.h"
#import "SpecialModel.h"
#import "SpecialTableViewCell.h"
#import "DetailCellViewController.h"
@interface SpecialTableViewController ()
/**/
@property (strong, nonatomic )NSMutableArray *specialArray;
/**/
@property (strong, nonatomic )NSMutableDictionary *specialDic;
@end

@implementation SpecialTableViewController
- (NSMutableArray *)specialArray{
    if (!_specialArray) {
        _specialArray = [[NSMutableArray alloc] init];
    }
    return _specialArray;
}
- (NSMutableDictionary *)specialDic{
    if (!_specialDic) {
        _specialDic = [[NSMutableDictionary alloc] init];
    }
    return _specialDic;
}
- (void)analyseData{
    NSString *urlString = [NSString stringWithFormat:SpecialUrl,_specialid];
    NSLog(@"%@",urlString);
    [NetWorkRequestManager requestWithType:GET urlString:urlString parDic:@{} finish:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dictionary[_specialid][@"topics"];
        for (NSDictionary *dic0 in array) {
            for (NSDictionary *dic1 in dic0[@"docs"]) {
                SpecialModel *model = [[SpecialModel alloc] init];
                [model setValuesForKeysWithDictionary:dic1];
                [self.specialArray addObject:model];
            }
            [self.specialDic setValue:_specialArray forKey:dic0[@"tname"]];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

    } finishError:^(NSError *error) {
        NSLog(@"解析错误");
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self analyseData];
    [self.tableView registerNib:[UINib nibWithNibName:@"SpecialTableViewCell" bundle:nil] forCellReuseIdentifier:@"sCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSArray *array = self.specialDic.allKeys;
    return array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.specialDic.allKeys;
    NSString *key = array[section];
    NSArray *array0 = [self.specialDic valueForKey:key];
    return array0.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sCell"];
    if (!cell) {
        cell = [[SpecialTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"sCell"];
    }
    NSArray *array = self.specialDic.allKeys;
    NSString *key = array[indexPath.section];
    NSArray *array0 = [self.specialDic valueForKey:key];
    SpecialModel *model = array0[indexPath.row];
    [cell setValueWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSArray *array = self.specialDic.allKeys;
    NSString *title = [NSString stringWithFormat:@"%ld/%ld  %@",section+1,array.count,array[section]];
    return title;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = self.specialDic.allKeys;
    NSString *key = array[indexPath.section];
    NSArray *array0 = [self.specialDic valueForKey:key];
    SpecialModel *model = array0[indexPath.row];
    DetailCellViewController *detailVC = [[DetailCellViewController alloc] init];
    detailVC.urlString = model.url;
    detailVC.index = 2;
    //push 后隐藏 tabBar
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
